<template>
    <a-spin :spinning="confirmLoading">
        <LQueryForm v-if="showQueryForm" :queryTable="this"  :formAttributes="formAttributes"></LQueryForm>
        <slot name="listItem" :list="localDataSource"></slot>
        <a-pagination v-if="showPagination" v-model="localPagination.current" style="float:right;margin:16px 0;"
            :total="localPagination.total" 
            :pageSizeOptions="['20', '30', '40']"
            :showTotal="localPagination.showTotal"
            :defaultPageSize="localPagination.pageSize"
            size="small"
            show-size-changer show-less-items 
            @change="changePag"
            @showSizeChange="showSizeChange"/>
    </a-spin>
</template>

<script>
import request from '@/utils/request'
import notification from 'ant-design-vue/es/notification'
import LQueryForm from '@/components/lt/form/LQueryForm'
 import { initDict } from '@/api/dict'
 let defaultPageSize = 15

export default {
    name:'Llist',
    components: {
        LQueryForm
    },
    props:{
        formAttributes:{
            type:Array,
            default:()=>[]
        },

        showPagination: {
            type: Boolean,
            default: true
        },
        axiosUrl:{
            type:String,
            required:true
        },
        axiosMethod:{
            type:String,
            default:'post'
        },
        initQuery:{
            type:Array,
            default:()=>[]
        },
        initOrder:{
            type:Array,
            default:()=>[]
        },
        showQueryForm: {
            type: Boolean,
            default: true
        },
        size:{
            type:String,
            default:'small'
        },
        isAutoLoad:{//是否自动查询
            type:Boolean,
            default:true
        },
        initDictCodes:String
    },
    data(){
        return{
        
            localDataSource:[],
            localPagination:{
                current: 1,
                pageSize: defaultPageSize,
                showTotal: (total, range) => {
                    return range[0] + "-" + range[1] + " 共" + total + "条"
                },
                showQuickJumper: false,
                showSizeChanger: false,
                total: 0,
                onChange: page => {
                    this.localPagination.current=page;
                    this.loadData(this.localPagination)
                },
            },

            dataCenter:{
                pageNo:1,
                pageSize:defaultPageSize,
                orderColumns:[],
                queryForm:[],
            },
            confirmLoading:false,
            //查询框的值
            queryFormVal:{},
            localInitQuery:null
        }
    },
    watch:{
        initQuery: {
            deep: true,
            handler(newV, oldV) {
                this.localInitQuery=newV
            }
        }
    },
    created(){
        this.localInitQuery = this.initQuery
        this.init()
    },
    methods:{
        async init(){
            if(this.initDictCodes){
                await initDict(this.initDictCodes)
            }
            if(this.isAutoLoad){
                this.searchQuery()
            }
        },
        refresh(){
             this.searchQuery()
        },
        searchQuery(){
            if(this.localPagination){
                this.localPagination.current=1
                this.localPagination.pageSize=defaultPageSize
            }
            this.loadData(this.localPagination)
        },
        changePag(page, pageSize){
            if(this.localPagination){
                this.localPagination.current=page
                this.localPagination.pageSize=pageSize
            }
            
            this.loadData(this.localPagination)
        },
        showSizeChange(current, size){
            if(this.localPagination){
                this.localPagination.current=current
                this.localPagination.pageSize=size
            }
            
            this.loadData(this.localPagination)
        },
        loadData(pagination){
            let that = this;
            if(!that.axiosUrl){
                that.$message.error("请设置请求地址:axiosUrl属性!")
                return
            }
            that.confirmLoading = true;
            that.getQueryParams(pagination);
            request({url: that.axiosUrl,method: that.axiosMethod,data:that.dataCenter}).then(r=>{
                if(r.success){
                    that.localDataSource =r.result.records || r.result
                    that.confirmLoading = false
                    if(that.showPagination){
                        let current = 1
                        let total =  0
                        if(r.result){
                            if(r.result.current){
                                current = r.result.current
                            }
                            if(r.result.total){
                                total = r.result.total
                            }
                            if(r.result.records){
                                if(r.result.records.current){
                                    current = r.result.records.current
                                }
                                if (r.result.records.total){
                                    total = r.result.records.total
                                }
                            }
                        }
                        that.localPagination =  Object.assign({}, that.localPagination, {
                            current: current, // 返回结果中的当前分页数
                            total: total, // 返回结果中的总记录数
                            showSizeChanger: ipagination.showSizeChanger,
                            pageSize: (pagination && pagination.pageSize) || that.localPagination.pageSize
                        })
                    }else{
                        that.localPagination = false
                    }
                }else{
                    notification.error({ message: '系统提示', description: r.message || "执行出现错误"}) 
                }
            }).finally(()=>{
                that.confirmLoading = false;
            })
        },
        getQueryParams(pagination, sorter){
            let that = this;
            that.queryParam=[]
            that.orderColumns=[]
            let queryParamField=[]//临时存放QUERYFORM字段信息
            let queryAttributes={}
            let betweenItem={} //存放范围查询的字段
            let stSign="_st"
            let etSign="_et"
            if(that.formAttributes && that.formAttributes.length>0){
                that.formAttributes.forEach(item=>{
                    queryAttributes[item['field']] = item
                    if(item['condition']=='between'){
                        betweenItem[item['field']+stSign] = {field:item['field'],condition:'ge'}
                        betweenItem[item['field']+etSign] = {field:item['field'],condition:'le'}
                    }
        
                })
                for(let key in that.queryFormVal){
                    if(that.queryFormVal[key]){
                        if(betweenItem[key] && that.queryFormVal[key]){
                            //如果有范围查询，并且有值
                            betweenItem[key]['val']=that.queryFormVal[key]
                            that.queryParam.push(betweenItem[key])
                            queryParamField.push(betweenItem[key]['field'])
                            continue
                        }

                        queryAttributes[key]['val']=that.queryFormVal[key]
                        that.queryParam.push(queryAttributes[key]) 
                        queryParamField.push(key)
                    }
                
                }
            }
            
            
            //默认查询放到最后并过滤掉重复的查询条件
            if(that.localInitQuery && that.localInitQuery.length>0){
                that.localInitQuery.forEach(v=>{
                    if((queryParamField.length==0?true:!queryParamField.includes(v['field']))){
                        that.queryParam.push(v)
                    } 
                })
            }


            //默认的排序放到最后并过滤掉重复的排序条件
            if(this.initOrder && this.initOrder.length>0){
                that.initOrder.forEach(v=>{
                    if(v['val'] && (!sorter?true:v['field']!=sorter.field)){
                        that.orderColumns.push(v)
                    } 
                })
            }
            that.dataCenter.orderColumns=that.orderColumns;
            that.dataCenter.queryForm=that.queryParam;
            that.dataCenter.pageNo = (pagination && pagination.current) || (that.showPagination && that.localPagination.current) 
            that.dataCenter.pageSize = (pagination && pagination.pageSize) || (that.showPagination && that.localPagination.pageSize) 
        },
        onActions(item,method){
            this.$emit(method,item)
        },
        onQueryFormChange(item){
            this.$emit("onQueryFormChange",item)
        },
        setInitQuery(initQuery){
            this.localInitQuery = initQuery
        }
    }
}
</script>